<?php
/**
 * 用餐管理模型
 *
 */
class Model_Order_Dinner extends Model_Base{
	
	private $userTable = '';
	private $departmentTable = '';
	private $visitorTable = '';
	private $orderCarTable = '';
	private $orderDinnerTable = '';
	private $documentTable = '';
	
	/**
	 * 构造函数
	 *
	 */
	public function __construct(){
		parent::__construct();
		$this->userTable = tabname('user');
		$this->departmentTable = tabname('department');
		$this->visitorTable =tabname('visitor');
		$this->orderCarTable = tabname('order_car');
		$this->orderDinnerTable = tabname('order_dinner');
		$this->documentTable = tabname('document');
	}
	
	public function countOrderDinner(){
		return $this->db->count($this->orderDinnerTable,array('is_canceled'=>0));
	}
	
	public function listOrderDinner($start=null,$length=null){
		$sql = "SELECT * FROM ".$this->orderDinnerTable." WHERE is_canceled = 0 ORDER BY id DESC";
		if($length > 0){
			$sql .= " LIMIT $start,$length";
		}
		$lists = $this->db->fetchAll($sql);
		foreach ($lists as $key=>$row){
			$lists[$key]['num'] = "DIN".str_pad(intval($row['id']), 10, "0", STR_PAD_LEFT);
			if ($row['class']==0){
				$lists[$key]['classname'] = "早餐";
			}
			elseif ($row['class']==1){
				$lists[$key]['classname'] = "午餐";
			}
			elseif ($row['class']==2){
				$lists[$key]['classname'] = "晚餐";
			}
			else{
				$lists[$key]['classname'] = "工作餐";
			}
			
		}
		return $lists;
	}
	
	public function comboboxUser(){
		$sql = "SELECT id, sirname, givenname"
				." FROM ".$this->userTable
				." WHERE is_deleted =0";
	
		$lists = $this->db->fetchAll($sql);
		foreach ($lists as $key=>$row){
			$lists[$key]['text'] = $row['sirname'].$row['givenname'];
		}
		return $lists;
	}
	
	public function comboboxDepartment(){
		$sql = "SELECT id, name"
				." FROM ".$this->departmentTable
				." WHERE is_deleted =0";
	
		$lists = $this->db->fetchAll($sql);
		foreach ($lists as $key=>$row){
			$lists[$key]['text'] = $row['name'];
		}
		return $lists;
	}
	
	public function comboboxVisitor(){
		$sql = "SELECT id, name"
				." FROM ".$this->visitorTable
				." WHERE is_deleted =0";
	
		$lists = $this->db->fetchAll($sql);
		foreach ($lists as $key=>$row){
			$lists[$key]['text'] = $row['name'];
		}
		return $lists;
	}
	
	public function getUname($id){
		$sql = "SELECT sirname, givenname"
				." FROM ".$this->userTable
				." WHERE id =".$id;
	
		$row = $this->db->fetchRow($sql);
		return $row['sirname'].$row['givenname'];
	}
	
	public function getDname($id){
		$sql = "SELECT name"
				." FROM ".$this->departmentTable
				." WHERE id =".$id;
	
		$row = $this->db->fetchRow($sql);
		return $row['name'];
	}
	
	public function getVname($id){
		$sql = "SELECT name"
				." FROM ".$this->visitorTable
				." WHERE id =".$id;
	
		$row = $this->db->fetchRow($sql);
		return $row['name'];
	}
	
	public function addOrderDinner($sets){
	$this->db->insert($this->orderDinnerTable,$sets);
		$ordercar_id = $this->db->lastInsertId();
		if($ordercar_id>0){
			return true;
		}
		else{
			return false;
		}
	}
	
	public function updateOrderDinner($id,$sets){
		if(empty($id)){
			return false;
		}else{
			$ret = $this->db->update($this->orderDinnerTable,$sets,array('id'=>$id));
			if($ret){
				return true;
			}
			else{
				return false;
			}
		}
	}
	
	public function delOrderDinner($id){
		if(empty($id)){
			return false;
		}else{
			$ret = $this->db->update($this->orderDinnerTable,array('is_canceled'=>1),array('id'=>$id));
			if($ret){
				return true;
			}
			else{
				return false;
			}
		}
	}
}
